package com.xdap.materialselect.util;

import com.xdap.motor.entity.SnowflakeIdWorker;
import com.xdap.motor.utils.StringUtils;
import com.xdap.runtime.service.RuntimeAppContextService;
import com.xdap.materialselect.entity.po.SystemCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @Description:
 * @Auther:huhanrui
 * @Date:2025/7/17 10:36
 */
@Component
public class MaSelectCommonUtil {

    @Autowired
    private RuntimeAppContextService runtimeAppContextService;


    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    public void buildSystemFields(SystemCommon systemCommon){
        String currentTenantId = runtimeAppContextService.getCurrentTenantId();
        String currentUserId;
        try {
            currentUserId = runtimeAppContextService.getCurrentUserId();
        }catch (Exception e){
            currentUserId ="system";
        }

        //系统字段
        systemCommon.setId(snowflakeIdWorker.nextId());
        systemCommon.setDocumentId(snowflakeIdWorker.nextId());
        systemCommon.setTenantId(currentTenantId);
//        systemCommon.setStatus("COMPLETED");
        systemCommon.setCreationDate(new Date());
        systemCommon.setLastUpdateDate(new Date());
        systemCommon.setLastUpdatedBy(currentUserId);
        systemCommon.setCreatedBy(currentUserId);
        systemCommon.setOwner(currentUserId);
    }

    public static String insertBracket(String s){
        if (StringUtils.isBlank(s)){
            return s;
        }
        return "[\"" + s + "\"]";

    }

    /**
     * 去除["和"]
     * @param s
     * @return
     */
    public static String deleteBracket(String s){
        if (Objects.isNull(s)){
            return null;
        }
        return s.replaceAll("\\[","").replaceAll("]","").replaceAll("\"","");

    }

}
